﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Ithalat
{
    public partial class CrossCheck : XForm
    {
        public CrossCheck()
        {
            InitializeComponent();
        }

        private void CrossCheck_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetCrossCheck");
            gridView1.AddDetailGrid(gridView2, "REFERANS", 1);
            gridView1.AddDetailGrid(gridView3, "REFERANS", 2);

            gridView2.AddColumn("Confirmed", typeof(int), "IIF(GercekTutar>-1,1,0)");
            gridView1.AddColumn("Confirmed", typeof(int), "SUM(Child({FK}).Confirmed)");

            //gridView2.SutunEkle("sorunlu", typeof(int), "IIF((ISNULL(GercekTutar,0)-ISNULL(WSTutar,0)>0.1 OR ISNULL(GercekTutar,0)-ISNULL(WSTutar,0)<-0.1) AND (ISNULL(Fark,0)>0.1 OR ISNULL(Fark,0)<-0.1), 1, 0)");
            //gridView1.SutunEkle("sorunlu", typeof(int), "MAX(Child({FK}).sorunlu)");

            gridView1.FormatAsNumber(0, "Confirmed");
            gridView1.FormatAsNumber(2, "WSMasraf", "ExcMasraf", "FARK");
            gridView2.FormatAsNumber(2, "ExcTutar", "WSTutar", "FARK", "GercekTutar");
            gridView3.FormatAsNumber(2, "ExcTutar", "WSTutar");

            gridView2.Columns["GercekTutar"].DisplayFormat.FormatString = "#,##0.00;-#,##0.00;0.00";

            gridView1.AddSummary("WSMasraf", "ExcMasraf", "FARK");
            gridView2.AddSummary("ExcTutar", "WSTutar", "FARK", "GercekTutar");
            gridView3.AddSummary("ExcTutar", "WSTutar");
            gridView2.MakeColumnInvisible("FARK", "Confirmed");

            gridView2.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gridView2.ShowingEditor += gridView2_ShowingEditor;

            gridView1.SetViewCaption("Özet");
            gridView2.SetViewCaption("Karşılaştırma");
            gridView3.SetViewCaption("Excel ve Webservisindeki farklılıklar");
            splitContainerControl2.AutoSize();
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;

            gridView1.MakeRowSelectable();
            gridView1.MakeReadOnly("REFERANS");
            gridView2.MakeReadOnly("HesapPlaniId", "GercekTutar");
            gridView3.MakeReadOnly();

            riHyperLink.SingleClick = false;
            gridView1.Columns["Icerik"].ColumnEdit = riHyperLink;
            gridView1.RowCellClick += gridView1_RowCellClick;

            gridView2.RowStyle += gridView2_RowStyle;
            gridView2.OptionsView.EnableAppearanceOddRow =
            gridView2.OptionsView.EnableAppearanceEvenRow = false;

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gridView2.DisableRowDelete();
            gridView2.DataTable().TableNewRow += CrossCheck_TableNewRow;
        }

        void CrossCheck_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["REFERANS"] = gridView1.GetFocusedDataRow()["REFERANS"];
        }

        void gridView2_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (gridView2.FocusedRowHandle < 0)
                e.Cancel = false;
            else
                e.Cancel = (gridView2.GetFocusedDataRow()["ExcTutar"].ToDecimal() == gridView2.GetFocusedDataRow()["WSTutar"].ToDecimal()) &&
                    gridView2.GetFocusedDataRow()["ExcTutar"].ToDecimal()>0;
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView2.EndEditing();
            string str = gridView2.SerializeChanges();
            ExecuteNonQuery("UpdIthalat_ConfirmedFigures", str);
        }
        RepositoryItemHyperLinkEdit riHyperLink = new RepositoryItemHyperLinkEdit();

        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            Cursor.Current = Cursors.WaitCursor;

            if (e.Column.FieldName == "Icerik")
            {
                DataRow dr = gridView1.GetDataRow(e.RowHandle);
                string REFERANS = dr["REFERANS"].ToStr();
                if (REFERANS != string.Empty &&
                    dr["Icerik"].ToStr() != string.Empty)
                {
                    DataTable dtDosya = ExecuteDataTable("GetIthalatDosya", REFERANS);

                    byte[] bytes = (byte[])dtDosya.Rows[0]["Dosya"];
                    string FileName = dtDosya.Rows[0]["FileName"].ToStr();

                    DevTeam.FileManager.RunTempFile(FileName, bytes);
                }
            }

            Cursor.Current = Cursors.Default;
        }


        void gridView2_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            bool farkli = gridView2.GetRowCellValue(e.RowHandle, "FARK").ToInt() != 0;
            //bool farkli2 = gridView2.GetRowCellValue(e.RowHandle, "WSTutar").ToInt() !=
            //    gridView2.GetRowCellValue(e.RowHandle, "GercekTutar").ToInt();
            if (farkli)
                e.Appearance.BackColor = Color.LightPink;
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();
        }
    }
}
